// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const TcbRouter = require('tcb-router')

const db = cloud.database()
const _ = db.command


const plCollection = db.collection('dianzan')
const MAX_LIMIT = 100

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const _openid = wxContext.OPENID

  const app = new TcbRouter({
    event
  })

  app.router('lost', async (ctx, next) => {
    let plList = await plCollection.skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
    ctx.body = plList
  })

  // 图片
  app.router('getListByOpenid', async (ctx, next) => {
    ctx.body = await plCollection.where({
      _openid: wxContext.OPENID,
      ip: 600
    }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
  })
  // 帖子
  app.router('getListByOpenid2', async (ctx, next) => {
    ctx.body = await plCollection.where({
      _openid: wxContext.OPENID,
      ip: 700
    }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
  })
  // 视频
  app.router('getListByOpenid3', async (ctx, next) => {
    ctx.body = await plCollection.where({
      _openid: wxContext.OPENID,
      ip: 800
    }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
  })
  app.router('detail', async (ctx, next) => {


    let plId = event.plId
    // 详情查询
    let detail = await plCollection.where({
      _id: plId
    }).get().then((res) => {
      return res.data
    })
    // 评论查询
    const countResult = await plCollection.count()
    const total = countResult.total
    let commentList = {
      data: []
    }
    if (total > 0) {
      const batchTimes = Math.ceil(total / MAX_LIMIT)
      const tasks = []
      for (let i = 0; i < batchTimes; i++) {
        let promise = db.collection('meitu-pl').skip(i * MAX_LIMIT)
          .limit(MAX_LIMIT).where({
            plId
          }).orderBy('createTime', 'desc').get()
        tasks.push(promise)
      }
      if (tasks.length > 0) {
        commentList = (await Promise.all(tasks)).reduce((acc, cur) => {
          return {
            data: acc.data.concat(cur.data)
          }
        })
      }

    }

    ctx.body = {
      commentList,
      detail,
    }

  })


  if (event.ip == 200) {
    // 点赞
    app.router('addzan', async (ctx, next) => {
      let addzan = await db.collection('dianzan').add({
        data: {
          _id: event.id + _openid,
          _openid,
          id: event.id,
          dianzan: event.dianzan
        }
      }).then((res) => {
        return res.data
      })
      let updatezan = await db.collection('tupian').where({
        _id: event.id
      }).update({
        data: {
          dz: _.inc(1)
        }
      }).then((res) => {
        return res.data
      })
      ctx.body = {
        addzan,
        updatezan
      }
    })
    // 取消点赞
    app.router('delzan', async (ctx, next) => {
      let delzan = await db.collection("dianzan").doc(event.id + _openid).remove({
      }).then((res) => {
        return res.data
      })
      let updatezan = await db.collection('tupian').where({
        _id: event.id
      }).update({
        data: {
          dz: _.inc(-1)
        }
      }).then((res) => {
        return res.data
      })
      ctx.body = {
        delzan,
        updatezan
      }
    })

  } else if (event.ip == 300) {
    // 点赞
    app.router('addzan', async (ctx, next) => {
      let addzan = await db.collection('dianzan').add({
        data: {
          _id: event.id + _openid,
          _openid,
          id: event.id,
          dianzan: event.dianzan
        }
      }).then((res) => {
        return res.data
      })
      let updatezan = await db.collection('tiezi').where({
        _id: event.id
      }).update({
        data: {
          dz: _.inc(1)
        }
      }).then((res) => {
        return res.data
      })
      ctx.body = {
        addzan,
        updatezan
      }
    })
    // 取消点赞
    app.router('delzan', async (ctx, next) => {
      let delzan = await db.collection("dianzan").doc(event.id + _openid).remove({
      }).then((res) => {
        return res.data
      })
      let updatezan = await db.collection('tiezi').where({
        _id: event.id
      }).update({
        data: {
          dz: _.inc(-1)
        }
      }).then((res) => {
        return res.data
      })
      ctx.body = {
        delzan,
        updatezan
      }
    })

  } else if (event.ip == 400) {
    // 点赞
    app.router('addzan', async (ctx, next) => {
      let addzan = await db.collection('dianzan').add({
        data: {
          _id: event.id + _openid,
          _openid,
          id: event.id,
          dianzan: event.dianzan
        }
      }).then((res) => {
        return res.data
      })
      let updatezan = await db.collection('shipin').where({
        _id: event.id
      }).update({
        data: {
          dz: _.inc(1)
        }
      }).then((res) => {
        return res.data
      })
      ctx.body = {
        addzan,
        updatezan
      }
    })
    // 取消点赞
    app.router('delzan', async (ctx, next) => {
      let delzan = await db.collection("dianzan").doc(event.id + _openid).remove({
      }).then((res) => {
        return res.data
      })
      let updatezan = await db.collection('shipin').where({
        _id: event.id
      }).update({
        data: {
          dz: _.inc(-1)
        }
      }).then((res) => {
        return res.data
      })
      ctx.body = {
        delzan,
        updatezan
      }
    })

  } else if (event.ip == 500) {
    // 点赞
    app.router('addzan', async (ctx, next) => {
      let addzan = await db.collection('dianzan').add({
        data: {
          _id: event.id + _openid,
          _openid,
          id: event.id,
          dianzan: event.dianzan
        }
      }).then((res) => {
        return res.data
      })
      let updatezan = await db.collection('tuijian').where({
        _id: event.id
      }).update({
        data: {
          dz: _.inc(1)
        }
      }).then((res) => {
        return res.data
      })
      ctx.body = {
        addzan,
        updatezan
      }
    })
    // 取消点赞
    app.router('delzan', async (ctx, next) => {
      let delzan = await db.collection("dianzan").doc(event.id + _openid).remove({
      }).then((res) => {
        return res.data
      })
      let updatezan = await db.collection('tuijian').where({
        _id: event.id
      }).update({
        data: {
          dz: _.inc(-1)
        }
      }).then((res) => {
        return res.data
      })
      ctx.body = {
        delzan,
        updatezan
      }
    })

  }

  // 状态
  // app.router('getzt', async(ctx, next) => {
  //   ctx.body = await plCollection.where({
  //       _id:event.id+_openid,
  //     }).get().then((res) => {
  //       console.log(event.id+_openid)

  //       return res.data
  //     })


  // })

  return app.serve()

}